package com.looeyWei;

public class BaoZiPu extends Thread{
    private BaoZi bz;

    // 必须要保证锁对象唯一，所以将锁对象baozi传进来
    public BaoZiPu(BaoZi bz){
        this.bz = bz;
    }

    @Override
    public void run() {
        int count = 0;
        while (true){  // 一直生产
            synchronized (bz){
                // 有包子，等待
                if (bz.flag == true){
                    try {
                        bz.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                // 有包子了
                if(count % 2 == 0){
                    // 生产薄皮
                    bz.pi = "薄皮";
                    bz.xian = "三鲜馅";
                }else{
                    bz.pi = "冰皮";
                    bz.xian = "牛肉馅";
                }
                count ++;
                System.out.println("包子正在生产：" + bz.pi + bz.xian + "包子");
                // 需要3秒钟
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                // 修改包子状态
                bz.flag = true;
                // 唤醒吃货
                bz.notify();
                System.out.println("包子已经生产好了！");
            }
        }
    }
}
